<Project>
  <PropertyGroup>
    <AsnXml />
    <_AsnXmlDiffCmd>%24%28command -v diff%29 -q -a</_AsnXmlDiffCmd>
    <_AsnXmlDiffCmd Condition="$([MSBuild]::IsOsPlatform('windows')) == 'true'">$(SystemRoot)\System32\fc.exe /a</_AsnXmlDiffCmd>
    <_AsnXmlDiffCmd Condition="$([MSBuild]::IsOsPlatform('osx')) == 'true'">diff</_AsnXmlDiffCmd>
  </PropertyGroup>
  <ItemGroup>
    <None Include="$(MSBuildThisFileDirectory)asn.xsd">
      <Link>Common\System\Security\Cryptography\Asn1\asn.xsd</Link>
    </None>
  </ItemGroup>

  <Target Name="CompileAsn" BeforeTargets="CoreCompile"
    Condition=" '@(AsnXml)' != '' "
    Inputs="@(AsnXml);$(MSBuildThisFileDirectory)asn.xslt"
    Outputs="%(Identity).cs">

    <PropertyGroup>
      <_AsnIntermediatePath>$([MSBuild]::NormalizeDirectory('$(IntermediateOutputPath)', 'asnxml'))</_AsnIntermediatePath>
    </PropertyGroup>

    <MakeDir Directories="$(_AsnIntermediatePath)" />

    <XslTransformation
      XslInputPath="$(MSBuildThisFileDirectory)asn.xslt"
      XmlInputPaths="@(AsnXml)"
      OutputPaths="@(AsnXml -> '$(_AsnIntermediatePath)%(filename).cs')" />

    <Exec
      IgnoreExitCode="true"
      StandardOutputImportance="Low"
      Command="$(_AsnXmlDiffCmd) @(AsnXml -> '&quot;$(_AsnIntermediatePath)%(filename).cs&quot;') @(AsnXml -> '&quot;%(Identity).cs&quot;')">
      <Output TaskParameter="ExitCode" ItemName="_AsnXmlDiffCode" />
    </Exec>

    <Copy
      Condition="'@(_AsnXmlDiffCode)' != '0'"
      SourceFiles="@(AsnXml -> '$(_AsnIntermediatePath)%(filename).cs')"
      DestinationFiles="@(AsnXml -> '%(Identity).cs')" />

    <Warning Condition="'@(_AsnXmlDiffCode)' != '0'" Text="AsnXml regenerated files, be sure to check them in: @(AsnXml -> '%(Identity).cs')" />
  </Target>
</Project>
